<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style-type: none;
        }
        li {
            height: 30px;
            width: 100px;
            background-color: #ccc;
            margin: 2px 0px;
            line-height: 30px;
            font-weight: 600;
            text-align: center;
            cursor: pointer;
        }
        ul {
            display: flex;
            justify-content: space-around;
            align-items: flex-start;
            flex-direction:row;
        }
        #box {
            border: 1px solid brown;
            height: 240px;
            width: 310px;
            margin: 0 auto;
        }
        .box {
            border: 1px solid brown;
            height: 200px;
            width: 305px;
            margin: 0 auto;
        }
        #box2 {
            border: 1px solid brown;
            height: 240px;
            width: 410px;
            margin: 0 auto;
        }
        .box2 {
            border: 1px solid brown;
            height: 200px;
            width: 405px;
            margin: 0 auto;
        }
        .topShow {
            display: block;
            background-color: rgba(165, 42, 42, 0.8);
        }
        p {
            display: none;
        }
        .show {
            display: block;
        }
    </style>
</head>
<body>
    <div id="box">
        <ul>
            <li class="topShow">一</li>
            <li>二</li>
            <li>三</li>
        </ul>
        <div class="box">
            <p class="show">abc</p>
            <p>1234</p>
            <p>qwe</p>
        </div>
    </div>

    <div id="box2">
        <ul>
            <li class="topShow">一</li>
            <li>二</li>
            <li>三</li>
            <li>四</li>
        </ul>
        <div class="box2">
            <p class="show">abc</p>
            <p>1234</p>
            <p>qwe</p>
            <p>四</p>
        </div>
    </div>

    <script>
        const box1 = document.querySelector("#box")
        const box2 = document.querySelector("#box2")

        function Tab(dom) {
            this.ul = dom.querySelector("ul")
            this.text = dom.querySelectorAll("p")
            this.switch()
        }

        Tab.prototype.switch = function () {
            this.ul.addEventListener("click", (e) => {
                if (e.target.nodeName == "LI") {
                    const index = Array.from(this.ul.children).indexOf(e.target);
                    for (let i = 0; i < this.ul.children.length; i++) {
                        this.ul.children[i].removeAttribute("class")
                        this.text[i].removeAttribute("class")
                    }
                    this.ul.children[index].setAttribute("class", "topShow")
                    this.text[index].setAttribute("class", "show")
                }
            })
        }

        let box_1 = new Tab(box1)
        let box_2 = new Tab(box2)
    </script>
</body>
</html>